<?php

/**
 * 此示例为 微信支付（H5 chooseWXPay 方式）
 * http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82
 */

require './config.php';

use Thenbsp\Wechat\Util\Util;
use Thenbsp\Wechat\OAuth;
use Thenbsp\Wechat\Jssdk;
use Thenbsp\Wechat\Wechat;
use Thenbsp\Wechat\Payment;
use Thenbsp\Wechat\Payment\Order;
use Thenbsp\Wechat\Payment\Business;
use Thenbsp\Wechat\Payment\Unifiedorder;
use Thenbsp\Wechat\Exception\PaymentException;

/**
 * 以 JSAPI 方式的付款需要获取用户 Openid
 */
if(false && !isset($_SESSION['openid']) ) {
    $o = new OAuth(APPID, APPSECRET);
    if( !isset($_GET['code']) ) {
        $o->authorize('http://qatest.qiezilife.com/m/weixin/authorization/?targetApp=pay');
    } else {
        $token = $o->getAccessToken($_GET['code']);
        $_SESSION['openid'] = $token->openid;
    }
}

/**
 * 第 1 步：获取 JSSDK 配置
 */
$o = new Jssdk(new Wechat(APPID, APPSECRET));
$jssdkConfig = $o->getConfig(array('chooseWXPay'), false);

/**
 * 第 1 步：定义商户
 * 设置商户证书路径请使用 setClientCert/setClientKey
 * $business->setClientCert('/your/path/to/apiclient_cert.pem');
 * $business->setClientKey('/your/path/to/apiclient_key.pem');
 */
$business = new Business();
$business->appid(APPID);
$business->appsecret(APPSECRET);
$business->mch_id(MCHID);
$business->mch_key(MCHKEY);
    $business->setClientCert('/data/apache_srv/qatest/system/config/cert/apiclient_cert.pem');
    $business->setClientKey('/data/apache_srv/qatest/system/config/cert/apiclient_key.pem');

// print_r($business->getParams());

/**
 * 第 2 步：定义订单
 */
$order = new Order();
$order->body('iphone 6 plus');
$order->out_trade_no(date('Y-m-dHis').mt_rand(10000, 99999));
$order->total_fee('1');
//$order->openid($_SESSION['openid']);
$order->openid('o6XLUsvPEQ9dX5DOLMSMGcqN_AHc');
$order->notify_url(Util::currentUrl());

// print_r($order->getParams());

/**
 * 第 3 步：统一下单
 */
$unifiedorder = new Unifiedorder();
$unifiedorder->setBusiness($business);
$unifiedorder->setOrder($order);

/**
 * 第 4 步：生成支付配置文件
 */
$o = new Payment($unifiedorder);
$configJSON = $o->getConfigJssdk();

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>Wechat SDK</title>
</head>
<body ontouchstart="">

<h1>微信支付测试&nbsp;&nbsp;<a href="javascript:;" onclick="window.location.reload()">刷新</a></h1>
<h4>Jssdk chooseWXPay 方式：</h4>
<button type="button" onclick="WXPayment()" style="font-size:16px;height:38px;">支付 ￥<?php echo ($order->getParams('total_fee') / 100); ?> 元</button>

<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
/**
 * 注入 JSSDK 配置
 */
wx.config(<?php echo $jssdkConfig; ?>);

/**
 * 点击支付按款
 */
var WXPayment = function() {

    var config = <?php echo $configJSON; ?>;

    config.success = function() {
        alert('支付成功！');
    }

    config.cancel = function() {
        alert('用户取消成功！');
    }

    wx.chooseWXPay(config);
}
</script>
</body>
</html>